package cur.nature.admin.service;

import cur.nature.admin.dao.Admin_ResourceDao;
import cur.nature.framework.mvc.crud.CrudService;
import cur.nature.admin.model.vo.Admin_ResourceVO;
import cur.nature.admin.model.po.Admin_ResourcePO;
import cur.nature.framework.mvc.tree.Menu;
import cur.nature.framework.mvc.tree.TreeNode;
import cur.nature.framework.mvc.tree.TreeUtil;
import cur.nature.framework.core.util.ObjectUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单分类表 Service
 * 作者：Auto Generator By wujian
 * 生成日期：2023-07-09 18:05:53
 */
@Service
public class Admin_ResourceService extends CrudService<Admin_ResourcePO, Admin_ResourceVO, Admin_ResourceDao> {
    public Admin_ResourceService(Admin_ResourceDao baseDao) {
        super(baseDao, Admin_ResourcePO.class, Admin_ResourceVO.class);
    }

    /**
     *
     * @param param
     * @return
     */
    public Admin_ResourceVO.list_Result list(Admin_ResourceVO.list_Param param) throws Exception {
        Admin_ResourceVO.list_Result result = new Admin_ResourceVO.list_Result();
        Menu topNode = new Menu("0", param.getNodeText());
        result.setTreeNode(topNode);
        // 查询出所有的资源
        List<Admin_ResourceVO> list = getDao().queryByCond(param);
        if(ObjectUtil.isNotEmpty(list)){
            //转换为树节点
            List<TreeNode> treeNodeList = new ArrayList<>();
            for (Admin_ResourceVO base : list) {
                Menu node = new Menu(base.getId()+"",base.getParentId(),base.getName(),base);
                node.getMeta().setTitle(base.getName());
                node.setName(base.getId()+"");
                node.setPath(base.getPath());
                node.setComponent(base.getCompPath());
                treeNodeList.add(node);
            }
//            List<TreeNode> treeNodeList = list.stream().map(base -> new TreeNode(base.getId()+"",base.getParentId(),base.getName(),base))
//                    .collect(Collectors.toList());
            //组装树
            TreeUtil.packTree(treeNodeList, topNode);
        }

        return result;
    }

}

